package com.fsi.client.util;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;

/**
 * @author Alejandro D. Garin
 */
public class BasicFileUploadPopup {

	private final DialogBox dialogBox = new DialogBox();

	private final FormPanel form = new FormPanel();

	private FileUpload fileUpload = new FileUpload();

	private String fileUploadName = "photoupload";
	private final HorizontalPanel titlePanel;
	private final Label titleLbl = new Label("Photo Title");
	private final TextBox photoTitle;
	private final HorizontalPanel buttonPanel;
	private final Button closeButton = new Button("Close");
	private final Button submitButton = new Button("Submit");

	private String actionURL;

	public BasicFileUploadPopup() {
		titlePanel = new HorizontalPanel();
		photoTitle = new TextBox();
		buttonPanel = new HorizontalPanel();

		dialogBox.setAnimationEnabled(true);
		dialogBox.setAutoHideEnabled(false);
		dialogBox.setText("Select an image to upload");

		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);

		photoTitle.setText("");
		titlePanel.add(titleLbl);
		titlePanel.add(photoTitle);

		buttonPanel.add(closeButton);
		buttonPanel.add(submitButton);

		FlowPanel flow = new FlowPanel();
		flow.getElement().getStyle().setProperty("padding", "5px");
		flow.add(form);
		flow.add(titlePanel);
		flow.add(new HTML("<hr />"));
		flow.add(buttonPanel);

		this.prepareFileUpload();

		dialogBox.setWidget(flow);

		setModal(true);

		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				close();
			}
		});

		submitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				form.submit();
				close();
			}
		});

		addSubmitHandler(new SubmitHandler() {

			@Override
			public void onSubmit(SubmitEvent event) {
				String photoTitle = getPhotoTitle().trim();
				MyLog.log("ACTION URL: " + actionURL + photoTitle);
				form.setAction(actionURL + photoTitle);
			}
		});

		addSubmitCompleteHandler(new SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				MyLog.log("on sumbit complete");
				// close();
			}
		});
	}

	/**
	 * Adds a {@link SubmitCompleteEvent} handler.
	 * 
	 * @param handler
	 *            the handler
	 * @return the handler registration used to remove the handler
	 */
	public HandlerRegistration addSubmitCompleteHandler(
			SubmitCompleteHandler handler) {
		return form.addSubmitCompleteHandler(handler);
	}

	/**
	 * Adds a {@link SubmitEvent} handler.
	 * 
	 * @param handler
	 *            the handler
	 * @return
	 * @return the handler registration used to remove the handler
	 */
	public HandlerRegistration addSubmitHandler(SubmitHandler handler) {
		return form.addSubmitHandler(handler);
	}

	/**
	 * set the form name (default = file)
	 * 
	 * @param name
	 */
	public void setFileUploadFormName(String name) {
		fileUploadName = name;
		this.fileUpload.setName(name);
	}

	/**
	 * 
	 * Set the caption of the file upload popup
	 */
	public void setCaption(String caption) {
		dialogBox.setText(caption);
	}

	/**
	 * 
	 * @see com.google.gwt.user.client.ui.PopupPanel#center()
	 */
	public void center(String actionURL) {
		init();
		this.actionURL = actionURL;
		dialogBox.center();
	}

	/**
	 * @param modal
	 * @see com.google.gwt.user.client.ui.PopupPanel#setModal(boolean)
	 */
	public void setModal(boolean modal) {
		dialogBox.setModal(modal);
	}

	/**
	 * 
	 * @see com.google.gwt.user.client.ui.DialogBox#show()
	 */
	public void show() {
		dialogBox.show();
	}

	/**
	 * clear the fileUpload
	 */
	public void init() {
		photoTitle.setText("");
	}

	private void prepareFileUpload() {
		init();
		fileUpload = new FileUpload();
		fileUpload.getElement().setAttribute("size", "40");

		fileUpload.setName(fileUploadName);

		form.setWidget(fileUpload);

		fileUpload.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				photoTitle.setFocus(true);
			}
		});
	}

	/**
	 * @param target
	 * @see com.google.gwt.user.client.ui.PopupPanel#showRelativeTo(com.google.gwt.user.client.ui.UIObject)
	 */
	public final void showRelativeTo(UIObject target) {
		dialogBox.showRelativeTo(target);
	}

	/**
	 * 
	 * @see com.google.gwt.user.client.ui.DialogBox#hide()
	 */
	public void close() {
		dialogBox.hide();
	}

	public String getPhotoTitle() {
		return photoTitle.getText();
	}

}
